package in.muton.edushare.pages.publicpages.coursepages;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareLogin;
import in.muton.edushare.pages.FooterLinks;
import in.muton.edushare.pages.publicpages.deptpages.DeptPublic;
import in.muton.edushare.pages.publicpages.univpages.UnivBasePublic;
import in.muton.edushare.pages.publicpages.univpages.UnivPublic;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;

public abstract class CourseBasePublic extends EduSharePage {
	public CourseBasePublic(PageParameters params) {
		super();
		getCourse(params);

		final CourseDataHandler cdh1 = new CourseDataHandler(courseModel, 0);
		PageParameters univParams = new PageParameters();
		univParams.put("0", cdh1.getOwnerUniv().getObject().getUniqueName());
		Link<Void> l = new BookmarkablePageLink<Void>("univLink",
				UnivPublic.class, univParams);
		add(l);
		l.add(new Label("univName", cdh1.getOwnerUniv().getObject()
				.getUniversityName()));

		add(new Label("courseName", courseModel.getObject().getCourseName()));
		
		PageParameters deptParams = new PageParameters();
		deptParams.put("0", cdh1.getOwner().getObject().getDeptName());
		deptParams.put("1", cdh1.getOwner().getObject().getDepartmentId()
				.toString());
		l = new BookmarkablePageLink<Void>("deptLink", DeptPublic.class,
				deptParams);
		l.add(new Label("deptName", cdh1.getOwner().getObject().getDeptName()));
		add(l);
		addLinks(params);
		add(CSSPackageResource.getHeaderContribution(UnivBasePublic.class,
				"PublicPage.css"));
		add(CSSPackageResource.getHeaderContribution(UnivBasePublic.class,
				"Structure.css"));
		add(new FooterLinks("footerLinks"));
		add(new Label("pagettl",courseModel.getObject().getCourseName()+" | " +
						cdh1.getOwnerUniv().getObject().getUniversityName()));
	}

	private void addLinks(PageParameters params) {
		for (String p : pageNames) {
			Link<Void> l = null;
			if (p.equals("courseBasePublic")) {
				add(l = new BookmarkablePageLink<Void>(p, CoursePublic.class,
						params));
			} else if (p.equals("courseFacultyPublic")) {
				PageParameters params2 = new PageParameters();
				params2.put("0", params.getString("0"));
				params2.put("1", params.getString("1"));
				params2.put("2", "faculty");
				add(l = new BookmarkablePageLink<Void>(p,
						CoursePeoplePublic.class, params2));
			} else if (p.equals("courseStudentsPublic")) {
				PageParameters params2 = new PageParameters();
				params2.put("0", params.getString("0"));
				params2.put("1", params.getString("1"));
				params2.put("2", "students");
				add(l = new BookmarkablePageLink<Void>(p,
						CoursePeoplePublic.class, params2));
			} else if (p.equals("login")) {
				if(EduShareSession.get().isLoggedIn()){
					add(l=new BookmarkablePageLink<Void>("login", URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH)));
					l.add(new Label("myHome", "My Home"));
				}else{
					add(l = new BookmarkablePageLink<Void>("login",EduShareLogin.class));
					l.add(new Label("myHome", "Login"));
				}
			}
			if (p.equals(getPageName(params))) {
				l.add(new SimpleAttributeModifier("class", "pubMenuSel"));
			}
		}
	}

	private void getCourse(PageParameters params) {
		int courseId = params.getInt("1", 0);
		if (courseId > 0) {
			Courses course = new CoursesDAO(getSessionORMInterface())
					.getCourseById(courseId);
			if (course != null) {
				courseModel = (new CayenneModel<Courses>(course));
				if (new CourseInstancesDAO(getSessionORMInterface())
						.getActiveInst(course) != null)
					cInstModel = new CayenneModel<CourseInstances>(
							new CourseInstancesDAO(getSessionORMInterface())
									.getActiveInst(course));
				return;
			}
		}
		throw new EduShareRuntimeException("Unable to find course");
	}

	public IModel<Courses> getCourseModel() {
		return courseModel;
	}

	public abstract String getPageName(PageParameters params);

	protected IModel<CourseInstances> cInstModel = null;
	private IModel<Courses> courseModel = null;

	public static final String[] pageNames = { "courseBasePublic",
			"courseStudentsPublic", "courseFacultyPublic", "login" };
}
